package com.czy.owner.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.GetCommentModel;
import com.czy.owner.entity.GuessYouLikeModel;
import com.czy.owner.entity.HeadLineModel;
import com.czy.owner.ui.order.ReturnOrderDetail;
import com.czy.owner.utils.ImagePagerUtilActivity;
import com.czy.owner.utils.TimeUtils;
import com.czy.owner.utils.glide.GlideUtils;
import com.czy.owner.widget.RoundImageView;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;

/**
 * Created by wangyang on 2017/5/15 0015.
 */

public class GetCommenOrderAdapter extends RecyclerArrayAdapter<GetCommentModel> {
    private Context context;

    public GetCommenOrderAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new CommenViewHolder(parent);
    }

    class CommenViewHolder extends BaseViewHolder<GetCommentModel> {
        private RoundImageView roundImgview;
        private TextView tvUserName;
        private TextView tvSpecGroupKey;
        private TextView tvNote;
        private ImageView imgProduct;
        private TextView tvProductDescription;
        private TextView tvProductPrice;
        private RatingBar ratingbarEvaluate;
        private EasyRecyclerView rvImg;
        private SeeEvaluateImgAdapter adapter;
        private ArrayList<String> picListRec = new ArrayList<>();

        public CommenViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_get_commen_order);
            roundImgview = $(R.id.roundimgview);
            tvUserName = $(R.id.tv_user_name);
            tvSpecGroupKey = $(R.id.tv_specgroupkey);
            tvNote = $(R.id.tv_note);
            imgProduct = $(R.id.img_product);
            tvProductDescription = $(R.id.tv_product_description);
            tvProductPrice = $(R.id.tv_product_price);
            ratingbarEvaluate = $(R.id.ratingbar_evaluate);
            rvImg = $(R.id.rv_img);
        }

        @Override
        public void setData(GetCommentModel data) {
            super.setData(data);
            tvUserName.setText(data.getNickName());
            tvSpecGroupKey.setText(TimeUtils.formatTimeDifference3(data.getCommentTime())+"  "+data.getSpecValue());
            tvNote.setText(data.getContent());
            tvProductDescription.setText(data.getGoodsName());
            tvProductPrice.setText("￥" + String.format("%.2f", data.getPrice()));
            ratingbarEvaluate.setRating(data.getStar());


            rvImg.setAdapter(adapter = new SeeEvaluateImgAdapter(context));
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 5);
            rvImg.setLayoutManager(gridLayoutManager);
            adapter.addAll(data.getPics());

            if (data.getPics().size() > 0) {
                for (int i = 0; i < data.getPics().size(); i++) {
                    picListRec.add(data.getPics().get(i).getResUrl());
                }
            }

            adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position, Object o) {
                    ImagePagerUtilActivity.ImageSize imageSize = new ImagePagerUtilActivity.ImageSize(view.getMeasuredWidth(), view.getMeasuredHeight());
                    ImagePagerUtilActivity.startImagePagerActivity(context, picListRec, position, imageSize);
                }
            });


            GlideUtils.loadImage(context, data.getUserLogo(), roundImgview, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_icon, R.mipmap.default_icon);

            GlideUtils.loadImage(context, data.getGoodsLogo(), imgProduct, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_icon, R.mipmap.default_icon);
        }
    }


    class SeeEvaluateImgAdapter extends RecyclerArrayAdapter<GetCommentModel.PicsBean> {
        private Context context;

        public SeeEvaluateImgAdapter(Context context) {
            super(context);
            this.context = context;
        }

        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            return new SubImgViewHolder(parent, context);
        }

        class SubImgViewHolder extends BaseViewHolder<GetCommentModel.PicsBean> {

            private ImageView imgSub;


            public SubImgViewHolder(ViewGroup parent, Context ctx) {
                super(parent, R.layout.item_see_evaluate_img);
                imgSub = $(R.id.img_sub);
            }

            @Override
            public void setData(GetCommentModel.PicsBean data) {
                super.setData(data);
                GlideUtils.loadImage(context, data.getThumbnailUrl(), imgSub, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                    @Override
                    public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                    }

                    @Override
                    public void onLoadingError(String source, Exception e) {

                    }
                }, R.mipmap.default_icon, R.mipmap.default_icon);
            }
        }
    }


}
